

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>geoslurp.dataset.RasterBase &mdash; geoslurp v1.1.0 documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  

  

  
        <link rel="index" title="Index"
              href="../../../genindex.html"/>
        <link rel="search" title="Search" href="../../../search.html"/>
    <link rel="top" title="geoslurp v1.1.0 documentation" href="../../../index.html"/>
        <link rel="up" title="Module code" href="../../index.html"/> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> geoslurp
          

          
          </a>

          
            
            
              <div class="version">
                1.1.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../introduction.html">An introduction to Geoslurp</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation of the geoslurp package and setting up the PostgreSQL instance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../confsettings.html">Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../examples.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference.html">Reference Documentation</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">geoslurp</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>geoslurp.dataset.RasterBase</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for geoslurp.dataset.RasterBase</h1><div class="highlight"><pre>
<span></span><span class="c1"># This file is part of geoslurp.</span>
<span class="c1"># geoslurp is free software; you can redistribute it and/or</span>
<span class="c1"># modify it under the terms of the GNU Lesser General Public</span>
<span class="c1"># License as published by the Free Software Foundation; either</span>
<span class="c1"># version 3 of the License, or (at your option) any later version.</span>

<span class="c1"># geoslurp is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>
<span class="c1"># Lesser General Public License for more details.</span>

<span class="c1"># You should have received a copy of the GNU Lesser General Public</span>
<span class="c1"># License along with geoslurp; if not, write to the Free Software</span>
<span class="c1"># Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA</span>

<span class="c1"># Author Roelof Rietbroek (roelof@geod.uni-bonn.de), 2019</span>


<span class="kn">from</span> <span class="nn">geoslurp.dataset.dataSetBase</span> <span class="k">import</span> <span class="n">DataSet</span>
<span class="kn">from</span> <span class="nn">geoslurp.config.slurplogger</span> <span class="k">import</span> <span class="n">slurplogger</span>
<span class="kn">from</span> <span class="nn">geoslurp.datapull.uri</span> <span class="k">import</span> <span class="n">findFiles</span><span class="p">,</span> <span class="n">UriFile</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">Column</span><span class="p">,</span><span class="n">Integer</span><span class="p">,</span><span class="n">String</span><span class="p">,</span><span class="n">Float</span>
<span class="kn">from</span> <span class="nn">geoalchemy2</span> <span class="k">import</span> <span class="n">Raster</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">func</span><span class="p">,</span><span class="n">select</span><span class="p">,</span><span class="n">text</span>
<span class="kn">from</span> <span class="nn">osgeo</span> <span class="k">import</span> <span class="n">gdal</span>

<div class="viewcode-block" id="RasterBase"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RasterBase.RasterBase">[docs]</a><span class="k">class</span> <span class="nc">RasterBase</span><span class="p">(</span><span class="n">DataSet</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Base class to load raster (tiles) into the postgis database&quot;&quot;&quot;</span>
    <span class="n">srid</span><span class="o">=</span><span class="kc">None</span> <span class="c1">#will try to find out the srid autmatically (but it&#39;s better to explicitly set this)</span>
    <span class="n">srcdir</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">rastregex</span><span class="o">=</span><span class="s2">&quot;.*&quot;</span>
    <span class="n">auxcolumns</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">outofdb</span><span class="o">=</span><span class="kc">False</span>
    <span class="n">regularblocking</span><span class="o">=</span><span class="kc">False</span>
    <span class="n">tiles</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">srid</span><span class="o">=</span><span class="mi">4326</span> <span class="c1">#default but can be overruled</span>
    <span class="n">bandname</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">overviews</span><span class="o">=</span><span class="kc">None</span>
    <span class="c1">#[ulx,xres,xskew,uly,yskew,yres]</span>
    <span class="n">geotransform</span><span class="o">=</span><span class="kc">None</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dbcon</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">dbcon</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">outofdb</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">srcdir</span><span class="p">:</span>
            <span class="c1">#expects stuff in the datadirectory</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">srcdir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dataDir</span><span class="p">()</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">outofdb</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">srcdir</span><span class="p">:</span>
            <span class="c1">#use the cacheDir</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">srcdir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cacheDir</span><span class="p">()</span>


<div class="viewcode-block" id="RasterBase.columns"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RasterBase.RasterBase.columns">[docs]</a>    <span class="k">def</span> <span class="nf">columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1">#construct the columns</span>
        <span class="n">cols</span><span class="o">=</span><span class="p">[</span><span class="n">Column</span><span class="p">(</span><span class="s2">&quot;id&quot;</span><span class="p">,</span><span class="n">Integer</span><span class="p">,</span><span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span><span class="n">Column</span><span class="p">(</span><span class="s2">&quot;uri&quot;</span><span class="p">,</span><span class="n">String</span><span class="p">),</span><span class="n">Column</span><span class="p">(</span><span class="s2">&quot;add_offset&quot;</span><span class="p">,</span><span class="n">Float</span><span class="p">),</span><span class="n">Column</span><span class="p">(</span><span class="s2">&quot;scale_factor&quot;</span><span class="p">,</span><span class="n">Float</span><span class="p">)]</span>
        <span class="c1"># possibly add auxiliary columns</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">auxcolumns</span><span class="p">:</span>
            <span class="n">cols</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">auxcolumns</span><span class="p">)</span>
        <span class="c1">#add raster column</span>
        <span class="n">cols</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Column</span><span class="p">(</span><span class="s2">&quot;rast&quot;</span><span class="p">,</span><span class="n">Raster</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">cols</span></div>

<div class="viewcode-block" id="RasterBase.register"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RasterBase.RasterBase.register">[docs]</a>    <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Checks the directory for updated raster files and updates them in the database&quot;&quot;&quot;</span>
        <span class="c1">#find all relevant files</span>
        <span class="n">newfiles</span><span class="o">=</span><span class="p">[</span><span class="n">UriFile</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">findFiles</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">srcdir</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">rastregex</span><span class="p">)]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dropTable</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">tiles</span><span class="p">:</span>
            <span class="c1">#expand a single raster in tiles</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">newfiles</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Don&#39;t know how to tile multiple input rasters&quot;</span><span class="p">)</span>
            <span class="c1">#possibly create a temporary table for storing the complete raster</span>
            <span class="c1"># we need to get a session which is bound to a single transaction unit so we can make use of temporay tables</span>
            <span class="n">trans</span><span class="p">,</span><span class="n">ses</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">transsession</span><span class="p">()</span>

            <span class="n">tmptable</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">createTable</span><span class="p">(</span><span class="s2">&quot;tmp_raster&quot;</span><span class="p">,[</span><span class="n">Column</span><span class="p">(</span><span class="s2">&quot;id&quot;</span><span class="p">,</span><span class="n">Integer</span><span class="p">,</span><span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span><span class="n">Column</span><span class="p">(</span><span class="s2">&quot;rast&quot;</span><span class="p">,</span><span class="n">Raster</span><span class="p">(</span><span class="n">spatial_index</span><span class="o">=</span><span class="kc">False</span><span class="p">))],</span><span class="n">temporary</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span><span class="n">bind</span><span class="o">=</span><span class="n">ses</span><span class="o">.</span><span class="n">get_bind</span><span class="p">())</span>
            <span class="n">meta</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">rastExtract</span><span class="p">(</span><span class="n">newfiles</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">entry</span><span class="o">=</span><span class="n">tmptable</span><span class="p">(</span><span class="n">rast</span><span class="o">=</span><span class="n">meta</span><span class="p">[</span><span class="s2">&quot;rast&quot;</span><span class="p">])</span>
            <span class="n">ses</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
            <span class="c1"># create the table but bind it to this session</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">createTable</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">columns</span><span class="p">(),</span><span class="n">session</span><span class="o">=</span><span class="n">ses</span><span class="p">)</span>
            <span class="c1"># fill the new table from the tiles</span>
            <span class="n">qry</span><span class="o">=</span><span class="n">select</span><span class="p">([</span><span class="n">func</span><span class="o">.</span><span class="n">ST_Tile</span><span class="p">(</span><span class="n">tmptable</span><span class="o">.</span><span class="n">rast</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">tiles</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">tiles</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span><span class="o">.</span><span class="n">label</span><span class="p">(</span><span class="s1">&#39;rast&#39;</span><span class="p">)])</span>
            <span class="n">ses</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="o">.</span><span class="n">__table__</span><span class="o">.</span><span class="n">insert</span><span class="p">()</span><span class="o">.</span><span class="n">from_select</span><span class="p">([</span><span class="s1">&#39;rast&#39;</span><span class="p">],</span><span class="n">qry</span><span class="p">))</span>
            <span class="k">del</span> <span class="n">meta</span><span class="p">[</span><span class="s2">&quot;rast&quot;</span><span class="p">]</span>
            <span class="c1">#also set the other column data</span>
            <span class="k">if</span> <span class="n">meta</span><span class="p">:</span>
                <span class="n">ses</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="o">.</span><span class="n">__table__</span><span class="o">.</span><span class="n">update</span><span class="p">()</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="o">**</span><span class="n">meta</span><span class="p">))</span>
            <span class="c1">#submit transaction</span>
            <span class="n">trans</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>

            <span class="k">for</span> <span class="n">uri</span> <span class="ow">in</span> <span class="n">newfiles</span><span class="p">:</span>
                <span class="n">meta</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">rastExtract</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">meta</span><span class="p">:</span>
                    <span class="c1">#don&#39;t register empty entries</span>
                    <span class="k">continue</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">table</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="c1">#create the table when it does not exist</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">createTable</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">columns</span><span class="p">())</span>

                <span class="bp">self</span><span class="o">.</span><span class="n">addEntry</span><span class="p">(</span><span class="n">meta</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_ses</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>

        <span class="c1">#fix the srid</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">srid</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_ses</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                <span class="n">text</span><span class="p">(</span><span class="s2">&quot;select UpdateRasterSRID(&#39;</span><span class="si">%s</span><span class="s2">&#39;::name,&#39;</span><span class="si">%s</span><span class="s2">&#39;::name,&#39;rast&#39;::name,</span><span class="si">%d</span><span class="s2">)&quot;</span><span class="o">%</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scheme</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">srid</span><span class="p">))</span>
            <span class="p">)</span>

        <span class="c1">#add/compute raster constraints</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_ses</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
            <span class="n">text</span><span class="p">(</span><span class="s2">&quot;select AddRasterConstraints(&#39;</span><span class="si">%s</span><span class="s2">&#39;::name,&#39;</span><span class="si">%s</span><span class="s2">&#39;::name,&#39;rast&#39;::name)&quot;</span><span class="o">%</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scheme</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">regularblocking</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_ses</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                <span class="n">text</span><span class="p">(</span><span class="s2">&quot;select AddRasterConstraints(&#39;</span><span class="si">%s</span><span class="s2">&#39;::name,&#39;</span><span class="si">%s</span><span class="s2">&#39;::name,&#39;rast&#39;::name,&#39;regular_blocking&#39;)&quot;</span><span class="o">%</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scheme</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>
            <span class="p">)</span>

        <span class="c1">#create overviews</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">overviews</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">overviews</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_ses</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">text</span><span class="p">(</span><span class="s2">&quot;select ST_CreateOverview(&#39;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&#39;::regclass, &#39;rast&#39;, </span><span class="si">%d</span><span class="s2">, &#39;Lanczos&#39;)&quot;</span><span class="o">%</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scheme</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span><span class="n">factor</span><span class="p">)))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">updateInvent</span><span class="p">()</span></div>

<div class="viewcode-block" id="RasterBase.rastExtract"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RasterBase.RasterBase.rastExtract">[docs]</a>    <span class="k">def</span> <span class="nf">rastExtract</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">uri</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;How things are extracted from the raster file (this may be overloaded in derived classes for more granular access&quot;&quot;&quot;</span>
        <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Extracting info from raster: </span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">))</span>

        <span class="n">bandnr</span><span class="o">=</span><span class="mi">1</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">outofdb</span><span class="p">:</span>
            <span class="c1">#explicitly open the gdal file to get the bounding box info</span>
            <span class="k">if</span> <span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;.nc&quot;</span><span class="p">):</span>
                <span class="n">prefix</span><span class="o">=</span><span class="s2">&quot;NETCDF:&quot;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">prefix</span><span class="o">=</span><span class="s2">&quot;&quot;</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bandname</span><span class="p">:</span>
                <span class="n">suffix</span><span class="o">=</span><span class="s2">&quot;:&quot;</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">bandname</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">suffix</span><span class="o">=</span><span class="s2">&quot;&quot;</span>

            <span class="n">fraster</span><span class="o">=</span><span class="n">gdal</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="o">+</span><span class="n">suffix</span><span class="p">)</span>

            <span class="c1"># if self.geotransform:</span>
                <span class="c1"># fraster.SetGeoTransform(self.geotransform)</span>
                <span class="c1"># nx=fraster.RasterYSize</span>
                <span class="c1"># ny=fraster.RasterXSize</span>
            <span class="c1"># else:</span>
            <span class="n">nx</span><span class="o">=</span><span class="n">fraster</span><span class="o">.</span><span class="n">RasterXSize</span>
            <span class="n">ny</span><span class="o">=</span><span class="n">fraster</span><span class="o">.</span><span class="n">RasterYSize</span>

            <span class="n">ulx</span><span class="p">,</span> <span class="n">xres</span><span class="p">,</span> <span class="n">xskew</span><span class="p">,</span> <span class="n">uly</span><span class="p">,</span> <span class="n">yskew</span><span class="p">,</span> <span class="n">yres</span>  <span class="o">=</span> <span class="n">fraster</span><span class="o">.</span><span class="n">GetGeoTransform</span><span class="p">()</span>
            <span class="c1"># uly, yres, yskew, ulx, xskew, xres  = fraster.GetGeoTransform()</span>


            <span class="n">emptyrast</span><span class="o">=</span><span class="n">func</span><span class="o">.</span><span class="n">ST_MakeEmptyRaster</span><span class="p">(</span><span class="n">nx</span><span class="p">,</span><span class="n">ny</span><span class="p">,</span><span class="n">ulx</span><span class="p">,</span><span class="n">uly</span><span class="p">,</span><span class="n">xres</span><span class="p">,</span><span class="n">yres</span><span class="p">,</span><span class="n">xskew</span><span class="p">,</span><span class="n">yskew</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">srid</span><span class="p">)</span>

            <span class="c1">#create an out of db rasterband</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="c1">#possibly we require a change of the path when we run postgresql in a container</span>
                <span class="n">outdbfile</span><span class="o">=</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">conf</span><span class="p">[</span><span class="s2">&quot;DataDir&quot;</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">conf</span><span class="p">[</span><span class="s2">&quot;InternalDataDir&quot;</span><span class="p">])</span>
            <span class="k">except</span><span class="p">:</span>
                <span class="n">outdbfile</span><span class="o">=</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span>

            <span class="n">rastb</span><span class="o">=</span><span class="n">fraster</span><span class="o">.</span><span class="n">GetRasterBand</span><span class="p">(</span><span class="n">bandnr</span><span class="p">)</span>
            <span class="n">nodata</span><span class="o">=</span><span class="n">rastb</span><span class="o">.</span><span class="n">GetNoDataValue</span><span class="p">()</span>
            <span class="n">scale</span><span class="o">=</span><span class="n">rastb</span><span class="o">.</span><span class="n">GetScale</span><span class="p">()</span>
            <span class="n">offset</span><span class="o">=</span><span class="n">rastb</span><span class="o">.</span><span class="n">GetOffset</span><span class="p">()</span>


            <span class="n">meta</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;rast&quot;</span><span class="p">:</span><span class="n">func</span><span class="o">.</span><span class="n">ST_AddBand</span><span class="p">(</span><span class="n">emptyrast</span><span class="p">,</span><span class="n">prefix</span><span class="o">+</span><span class="n">outdbfile</span><span class="o">+</span><span class="n">suffix</span><span class="p">,[</span><span class="n">bandnr</span><span class="p">],</span><span class="mi">0</span><span class="p">,</span><span class="n">nodata</span><span class="p">),</span>
                  <span class="s2">&quot;uri&quot;</span><span class="p">:</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="s2">&quot;add_offset&quot;</span><span class="p">:</span><span class="n">offset</span><span class="p">,</span><span class="s2">&quot;scale_factor&quot;</span><span class="p">:</span><span class="n">scale</span><span class="p">}</span>
            <span class="n">fraster</span><span class="o">=</span><span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1">#read directly from gdal format</span>
            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fid</span><span class="p">:</span>
                <span class="n">fbytes</span><span class="o">=</span><span class="n">fid</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
                <span class="n">meta</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;rast&quot;</span><span class="p">:</span><span class="n">func</span><span class="o">.</span><span class="n">ST_FromGDALRaster</span><span class="p">(</span><span class="n">fbytes</span><span class="p">,</span><span class="n">srid</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">srid</span><span class="p">),</span><span class="s2">&quot;uri&quot;</span><span class="p">:</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">}</span>

        <span class="k">return</span> <span class="n">meta</span></div></div>

</pre></div>

           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Roelof Rietbroek.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'v1.1.0',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>